import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const express = require('express')
import { mysqlObj } from '../../utils/mysql.js';

const taskRouter = express.Router()
/**
 * @api {get} /task task
 * @apiGroup task
 * @apiParam name 用户
 */
taskRouter.get('/', function (req, res) {
    res.writeHead(200, { 'Content-type': "text/html;charset=utf-8" })
    res.write('<p>任务管理首页</p>')
    res.end()
})
/**
 * @api {get} /task/insert 插入任务
 * @apiGroup task
 * @apiParam name 用户
 */
taskRouter.get('/insertTask', function (req, res) {
    res.writeHead(200, { 'Content-type': "text/html;charset=utf-8" })
    let content = req.query.content;
    let startTime = req.query.startTime;
    let endTime = req.query.endTime;
    let open_id_builder = req.query.open_id_builder;
    let open_id_receiver = req.query.open_id_receiver;

    mysqlObj.create().then((connct) => {
        let state = `insert into tasks(content,startTime,endTime,open_id_builder, open_id_receiver) values('${content}','${startTime}', '${endTime}','${open_id_builder}','${open_id_receiver}')`;
        connct.query(state, function (error, results, fields) {
            if (error) throw error;
            res.write("200");
            res.end();
        })
    })
})

/**
 * @api {get} /task/query 查询任务列表
 * @apiGroup task
 * @apiParam name 用户
 */
taskRouter.get('/getTaskList', function (req, res) {
    res.writeHead(200, { 'Content-type': "text/html;charset=utf-8" })
    let pageSize = Number(req.query.pageSize)
    let pageNum = Number(req.query.pageIndex)
    let index = Number((pageNum - 1) * pageSize)

    mysqlObj.create().then((connct) => {
        let state = `select * from tasks`;
        connct.query(state, function (error, results, fields) {
            if (error) throw error;
            let tempArray = results.splice(index, pageSize)
            let length = results.length;
            let json = {}
            json.list = tempArray
            json.length = length;
            res.write(JSON.stringify(json))
            res.end()
        })
    })
})
/**
 * 前台获取任务列表
 */
taskRouter.get('/getFrontTaskList', function (req, res) {
    let open_id_receiver = req.query.open_id_receiver;
    res.writeHead(200, { 'Content-type': "text/html;charset=utf-8" })
    mysqlObj.create().then((connct) => {
        let state = `select * from tasks where open_id_receiver='${open_id_receiver}'`;
        connct.query(state, function (error, results, fields) {
            if (error) throw error;
            let json = {};
            json.list = results;
            res.write(JSON.stringify(json))
            res.end()
        })
    })
})
/**
 * @api {get} /task/batchDelete batchDelete
 * @apiGroup task
 * @apiParam name 用户
 */
taskRouter.get('/batchDeleteTask', function (req, res) {
    res.writeHead(200, { 'Content-type': "text/html;charset=utf-8" })
    let taskIds = []
    for(let i in req.query) {
        taskIds.push(req.query[i]);
    }
    let str = taskIds.join();
    str = str.replace(/,/g, '\',')
    let state = `delete from tasks where task_id in ('${str}')`
    console.log(state)
    mysqlObj.create().then((conn) => {
        conn.query(state, function(error, results, fields) {
            if(error) throw error;
            console.log(results)
            res.write("200")
            res.end()
        })
    })
})

/**
 * @api {get} /task/query 查询列表
 * @apiGroup task
 * @apiParam name 用户
 */
taskRouter.get('/getTaskList', function (req, res) {
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" })
    mysqlObj.create().then((conn) => {
        conn.query('select * from tasks', function (error, results, field) {
            if (error) throw error;
            let json = {};
            json.list = results;
            res.write(JSON.stringify(json))
            res.end();
        })
    })
})

/**
 * @api {get} /task/updateTask 更新任务
 * @apiGroup task
 * @apiParam name 用户
 */
taskRouter.get('/updateTask', function (req, res) {
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" })
    let content = req.query.content;
    let task_id = req.query.task_id;
    mysqlObj.create().then((conn) => {
        let state = `update tasks set content='${content}' where task_id='${task_id}'`;
        conn.query(state, function (error, results, field) {
            if (error) throw error;
            res.write("200")
            res.end();
        })
    })
})

/**
 * 根据task_id查询
 */
taskRouter.get("/getTask", function (req, res) {
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" })
    let task_id = req.query.task_id;
    mysqlObj.create().then((conn) => {
        conn.query(`select * from tasks where task_id='${task_id}'`, function (error, results, fields) {
            if (error) throw error;
            let json = {};
            json.list = results;
            res.write(JSON.stringify(json))
            res.end()
        })
    })
})
// 接收传递过来的任务
taskRouter.get('/receiveTask', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>发送任务</p>")
    res.end()
})


export const task = taskRouter;